using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace M0
{
    public class GameButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
    {
        [SerializeField] private bool enable;
        public bool Enable
        {
            get { return enable; }
            set
            {
                enable = value;
                if (灰色材质求 != null)
                {
                    var images = transform.GetComponentsInChildren<Image>();
                    var Text = transform.GetComponentsInChildren<Text>();
                    foreach (var VARIABLE in images)
                    {
                        VARIABLE.material = enable ? null : 灰色材质求;
                    }
                    foreach (var VARIABLE in Text)
                    {
                        VARIABLE.material = enable ? null : 灰色材质求;
                    }
                }
            }
        }
        public float 放大缩小 = 0.8f;
        private float CurBig = 1f;

        [SerializeField] protected UnityEvent OnClickEvent;
        [SerializeField] private float speed = 0.1f;
        [SerializeField] private Material 灰色材质求;

        private float curBig;
        private float targetSclar;
        
        private void Update()
        {
            if (Math.Abs(targetSclar - curBig)>0.001f)
            {
                curBig = Mathf.Lerp(curBig, targetSclar, speed);
                if (Math.Abs(targetSclar - curBig) < 0.001f)
                {
                    curBig = targetSclar;
                }

                transform.localScale = curBig * Vector3.one;
            }
        }

        private void Awake()
        {
            CurBig = transform.localScale.x;
            curBig = CurBig;
            targetSclar = curBig;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            OnClickEvent?.Invoke();
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            targetSclar = 放大缩小;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            targetSclar = CurBig;
            
        }
        
        public void 代码OnClick()
        {
            OnClickEvent?.Invoke();
        }
    }
}

